﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace TrainingDemo.Util.NativeUtil
{
    class NativeApiHandler
    {

        #region Import part

        [DllImport("kernel32.dll")]
        public static extern IntPtr LoadLibrary(string dllToLoad);

        [DllImport("kernel32.dll")]
        public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

        [DllImport("kernel32.dll")]
        public static extern bool FreeLibrary(IntPtr hModule);

        #endregion



        #region Contruction/Destruction/Init

        NativeApiHandler() { }
        NativeApiHandler(string strLibraryPath)
        {
            m_strLibraryPath = strLibraryPath;
        }


        #endregion


        #region Private property

        /// <summary>
        /// remain path of library
        /// </summary>
        string m_strLibraryPath;

        /// <summary>
        /// Pointer to loaded library
        /// </summary>
        IntPtr m_pLibraryPointer;

        #endregion


        #region Private method


        int LoadLibrary()
        {
            try
            {
                m_pLibraryPointer = LoadLibrary(m_strLibraryPath);
            }
            catch (System.Exception ex)
            {
                return Const.AUTO_FALSE;
            }
            return Const.AUTO_OK;
        }

        int UnloadLibrary()
        {
            try
            {
                FreeLibrary(m_pLibraryPointer);
            }
            catch (System.Exception ex)
            {
                return Const.AUTO_FALSE;
            }
            return Const.AUTO_OK;
        }

        #endregion

    }
}
